###########################
#DATA ON POLITICAL PARTIES#
###########################

#MARIANA CARMO DUARTE

#AUGUST 2024

library(haven)
library(foreign)
library(scales)
library(lubridate)
library(tidyverse)
library(dplyr)
library(data.table)

setwd("")
polparties <- read.spss("MARPOR_ALL.sav", use.value.labels = TRUE, to.data.frame = TRUE)

#CREATE VARIABLES
#Year election
polparties$electionyear <- substr(polparties$date, 1, 4)
polparties$electionyear[polparties$date=="198211"] <- 1982.5
polparties$electionyear[polparties$date=="198911"] <- 1989.5
polparties$electionyear[polparties$date=="201206" & polparties$country=="greece"] <- 2012.5
polparties$electionyear[polparties$date=="201509"] <- 2015.5
polparties$electionyear[polparties$date=="201911"] <- 2019.5
head(polparties$electionyear)

#Country_year
setwd("")
polpargov <- read.spss("Elections_PARLGOV.sav", use.value.labels = TRUE, to.data.frame = TRUE)
polpargov$election_year[polpargov$election_date=="24/11/1982"] <- 1982.5
polpargov$election_year[polpargov$election_date=="05/11/1989"] <- 1989.5
polpargov$election_year[polpargov$election_date=="17/06/2012" & polpargov$country_name=="Greece        "] <- 2012.5
polpargov$election_year[polpargov$election_date=="20/09/2015"] <- 2015.5
polpargov$election_year[polpargov$election_date=="10/11/2019"] <- 2019.5

polpargov$country_electionyear <- paste0(polpargov$country_name_short, polpargov$election_year)
polparties$country_electionyear <- paste0(polparties$country, polparties$electionyear)
polparties$country_electionyear1 <- paste0(polparties$country, polparties$date)
polparties <- polparties %>% drop_na(per108, per110)

#Seat share
polparties$seat_share <- ((polparties$absseat)*100)/(polparties$totseats)
polparties$seat_share
polparties <- subset(polparties, seat_share > 0)

#VARIABLES EU-SALIENCE & EU POSITION
polparties$mentions108 <- round(polparties$per108/100*polparties$total)
summary(polparties$mentions108)

polparties$mentions110 <- round(polparties$per110/100*polparties$total)
summary(polparties$mentions110)

polparties$posEU <- log((polparties$mentions108 + 0.5) / (polparties$mentions110 + 0.5))
summary(polparties$posEU)

polparties$salEU <- log((polparties$mentions108 + polparties$mentions110 + 1) /2) 
summary(polparties$salEU)

#Variable EU Salience
polparties <- setDT(polparties)
polparties$EU_salience <- polparties$per108 + polparties$per110

#SALIENCE 
polparties$salience.w2 <- polparties$EU_salience*polparties$seat_share
polparties$eu_salience_ps2 <- ave(polparties$salience.w, polparties$country_electionyear, FUN = sum)/100
polparties$eu_salience_d2 <- polparties$eu_salience_ps/2

#Variable EU Polarisation
all.neg <- function(x) -1*abs(x)
polparties$per110_n <- all.neg(polparties$per110)
polparties$position <- (polparties$per108 - polparties$per110)
data_final2 <- polparties %>%                                        
  group_by(party_id, country_electionyear) %>%                         
  summarise_at(vars(position),            
               list(EU_position = mean))
polparties <- merge(data_final2, polparties, by=c("party_id","country_electionyear"), all.x = T)

#POLARISATION
polparties$averpos_p2 <- ave(polparties$EU_position, polparties$country_electionyear, FUN = mean)
polparties$dist_pol2 <- (polparties$EU_position - polparties$averpos_p2)^2
polparties$w.dist_pol2 <- polparties$seat_share*polparties$dist_pol2
polparties$sum_p2 <- ave(polparties$w.dist_pol2, polparties$country_electionyear, FUN = sum)
polparties$eu_polarisation_ps2 <- sqrt(polparties$sum_p2)


#ELECTION ID - PARLGOV

#Austria
polparties$election_id[polparties$country_electionyear=="austria1959"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1959"])
polparties$election_id[polparties$country_electionyear=="austria1962"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1962"])
polparties$election_id[polparties$country_electionyear=="austria1966"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1966"])
polparties$election_id[polparties$country_electionyear=="austria1970"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1970"])
polparties$election_id[polparties$country_electionyear=="austria1971"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1971"])
polparties$election_id[polparties$country_electionyear=="austria1975"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1975"])
polparties$election_id[polparties$country_electionyear=="austria1979"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1979"])
polparties$election_id[polparties$country_electionyear=="austria1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1983"])
polparties$election_id[polparties$country_electionyear=="austria1986"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1986"])
polparties$election_id[polparties$country_electionyear=="austria1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1990"])
polparties$election_id[polparties$country_electionyear=="austria1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1994"])
polparties$election_id[polparties$country_electionyear=="austria1995"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1995"])
polparties$election_id[polparties$country_electionyear=="austria1999"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT1999"])
polparties$election_id[polparties$country_electionyear=="austria2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT2002"])
polparties$election_id[polparties$country_electionyear=="austria2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT2006"])
polparties$election_id[polparties$country_electionyear=="austria2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT2008"])
polparties$election_id[polparties$country_electionyear=="austria2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT2013"])
polparties$election_id[polparties$country_electionyear=="austria2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT2017"])
polparties$election_id[polparties$country_electionyear=="austria2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="AUT2019"])

#Belgium
polparties$election_id[polparties$country_electionyear=="belgium1958"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1958"])
polparties$election_id[polparties$country_electionyear=="belgium1961"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1961"])
polparties$election_id[polparties$country_electionyear=="belgium1965"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1965"])
polparties$election_id[polparties$country_electionyear=="belgium1968"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1968"])
polparties$election_id[polparties$country_electionyear=="belgium1971"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1971"])
polparties$election_id[polparties$country_electionyear=="belgium1974"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1974"])
polparties$election_id[polparties$country_electionyear=="belgium1977"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1977"])
polparties$election_id[polparties$country_electionyear=="belgium1978"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1978"])
polparties$election_id[polparties$country_electionyear=="belgium1981"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1981"])
polparties$election_id[polparties$country_electionyear=="belgium1985"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1985"])
polparties$election_id[polparties$country_electionyear=="belgium1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1987"])
polparties$election_id[polparties$country_electionyear=="belgium1991"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1991"])
polparties$election_id[polparties$country_electionyear=="belgium1995"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1995"])
polparties$election_id[polparties$country_electionyear=="belgium1999"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL1999"])
polparties$election_id[polparties$country_electionyear=="belgium2003"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL2003"])
polparties$election_id[polparties$country_electionyear=="belgium2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL2007"])
polparties$election_id[polparties$country_electionyear=="belgium2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL2010"])
polparties$election_id[polparties$country_electionyear=="belgium2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL2014"])
polparties$election_id[polparties$country_electionyear=="belgium2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BEL2019"])

#Bulgaria
polparties$election_id[polparties$country_electionyear=="bulgaria1991"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR1991"])
polparties$election_id[polparties$country_electionyear=="bulgaria1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR1994"])
polparties$election_id[polparties$country_electionyear=="bulgaria1997"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR1997"])
polparties$election_id[polparties$country_electionyear=="bulgaria2001"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR2001"])
polparties$election_id[polparties$country_electionyear=="bulgaria2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR2005"])
polparties$election_id[polparties$country_electionyear=="bulgaria2009"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR2009"])
polparties$election_id[polparties$country_electionyear=="bulgaria2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR2013"])
polparties$election_id[polparties$country_electionyear=="bulgaria2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR2014"])
polparties$election_id[polparties$country_electionyear=="bulgaria2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="BGR2017"])

#Croatia
polparties$election_id[polparties$country_electionyear=="croatia2000"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HRV2000"])
polparties$election_id[polparties$country_electionyear=="croatia2003"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HRV2003"])
polparties$election_id[polparties$country_electionyear=="croatia2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HRV2007"])
polparties$election_id[polparties$country_electionyear=="croatia2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HRV2011"])
polparties$election_id[polparties$country_electionyear=="croatia2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HRV2015"])
polparties$election_id[polparties$country_electionyear=="croatia2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HRV2016"])
polparties$election_id[polparties$country_electionyear=="croatia2020"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HRV2020"])

#Cyrpus
polparties$election_id[polparties$country_electionyear=="cyprus1976"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CYP1976"])
polparties$election_id[polparties$country_electionyear=="cyprus1981"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CYP1981"])
polparties$election_id[polparties$country_electionyear=="cyprus1985"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CYP1985"])
polparties$election_id[polparties$country_electionyear=="cyprus1991"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CYP1991"])
polparties$election_id[polparties$country_electionyear=="cyprus1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CYP1996"])
polparties$election_id[polparties$country_electionyear=="cyprus2001"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CYP2001"])
polparties$election_id[polparties$country_electionyear=="cyprus2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CYP2006"])
polparties$election_id[polparties$country_electionyear=="cyprus2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CYP2011"])
polparties$election_id[polparties$country_electionyear=="cyprus2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CYP2016"])

#Czech Republic
polparties$election_id[polparties$country_electionyear=="czech republic1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE1990"])
polparties$election_id[polparties$country_electionyear=="czech republic1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE1992"])
polparties$election_id[polparties$country_electionyear=="czech republic1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE1996"])
polparties$election_id[polparties$country_electionyear=="czech republic1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE1998"])
polparties$election_id[polparties$country_electionyear=="czech republic2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE2002"])
polparties$election_id[polparties$country_electionyear=="czech republic2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE2006"])
polparties$election_id[polparties$country_electionyear=="czech republic2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE2010"])
polparties$election_id[polparties$country_electionyear=="czech republic2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE2013"])
polparties$election_id[polparties$country_electionyear=="czech republic2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="CZE2017"])

#Denmark
polparties$election_id[polparties$country_electionyear=="denmark1957"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1957"])
polparties$election_id[polparties$country_electionyear=="denmark1960"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1960"])
polparties$election_id[polparties$country_electionyear=="denmark1964"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1964"])
polparties$election_id[polparties$country_electionyear=="denmark1966"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1966"])
polparties$election_id[polparties$country_electionyear=="denmark1968"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1968"])
polparties$election_id[polparties$country_electionyear=="denmark1971"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1971"])
polparties$election_id[polparties$country_electionyear=="denmark1973"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1973"])
polparties$election_id[polparties$country_electionyear=="denmark1975"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1975"])
polparties$election_id[polparties$country_electionyear=="denmark1977"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1977"])
polparties$election_id[polparties$country_electionyear=="denmark1979"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1979"])
polparties$election_id[polparties$country_electionyear=="denmark1981"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1981"])
polparties$election_id[polparties$country_electionyear=="denmark1984"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1984"])
polparties$election_id[polparties$country_electionyear=="denmark1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1987"])
polparties$election_id[polparties$country_electionyear=="denmark1988"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1988"])
polparties$election_id[polparties$country_electionyear=="denmark1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1990"])
polparties$election_id[polparties$country_electionyear=="denmark1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1994"])
polparties$election_id[polparties$country_electionyear=="denmark1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK1988"])
polparties$election_id[polparties$country_electionyear=="denmark2001"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK2001"])
polparties$election_id[polparties$country_electionyear=="denmark2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK2005"])
polparties$election_id[polparties$country_electionyear=="denmark2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK2007"])
polparties$election_id[polparties$country_electionyear=="denmark2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK2011"])
polparties$election_id[polparties$country_electionyear=="denmark2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK2015"])
polparties$election_id[polparties$country_electionyear=="denmark2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DNK2019"])

#Spain
polparties$election_id[polparties$country_electionyear=="spain1977"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP1977"])
polparties$election_id[polparties$country_electionyear=="spain1979"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP1979"])
polparties$election_id[polparties$country_electionyear=="spain1982"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP1982"])
polparties$election_id[polparties$country_electionyear=="spain1986"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP1986"])
polparties$election_id[polparties$country_electionyear=="spain1989"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP1989"])
polparties$election_id[polparties$country_electionyear=="spain1993"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP1993"])
polparties$election_id[polparties$country_electionyear=="spain1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP1996"])
polparties$election_id[polparties$country_electionyear=="spain2000"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2000"])
polparties$election_id[polparties$country_electionyear=="spain2004"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2004"])
polparties$election_id[polparties$country_electionyear=="spain2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2008"])
polparties$election_id[polparties$country_electionyear=="spain2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2011"])
polparties$election_id[polparties$country_electionyear=="spain2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2015"])
polparties$election_id[polparties$country_electionyear=="spain2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2016"])
polparties$election_id[polparties$country_electionyear=="spain2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2019.5"])
polparties$election_id[polparties$country_electionyear=="spain2019.5"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ESP2019.5"])


#Estonia
polparties$election_id[polparties$country_electionyear=="estonia1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST1992"])
polparties$election_id[polparties$country_electionyear=="estonia1995"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST1995"])
polparties$election_id[polparties$country_electionyear=="estonia1999"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST1999"])
polparties$election_id[polparties$country_electionyear=="estonia2003"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST2003"])
polparties$election_id[polparties$country_electionyear=="estonia2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST2007"])
polparties$election_id[polparties$country_electionyear=="estonia2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST2011"])
polparties$election_id[polparties$country_electionyear=="estonia2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST2015"])
polparties$election_id[polparties$country_electionyear=="estonia2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="EST2019"])

#Finland
polparties$election_id[polparties$country_electionyear=="finland1958"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1958"])
polparties$election_id[polparties$country_electionyear=="finland1962"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1962"])
polparties$election_id[polparties$country_electionyear=="finland1966"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1966"])
polparties$election_id[polparties$country_electionyear=="finland1970"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1970"])
polparties$election_id[polparties$country_electionyear=="finland1972"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1972"])
polparties$election_id[polparties$country_electionyear=="finland1975"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1975"])
polparties$election_id[polparties$country_electionyear=="finland1979"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1979"])
polparties$election_id[polparties$country_electionyear=="finland1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1983"])
polparties$election_id[polparties$country_electionyear=="finland1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1987"])
polparties$election_id[polparties$country_electionyear=="finland1991"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1991"])
polparties$election_id[polparties$country_electionyear=="finland1995"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1995"])
polparties$election_id[polparties$country_electionyear=="finland1999"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN1999"])
polparties$election_id[polparties$country_electionyear=="finland2003"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN2003"])
polparties$election_id[polparties$country_electionyear=="finland2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN2007"])
polparties$election_id[polparties$country_electionyear=="finland2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN2011"])
polparties$election_id[polparties$country_electionyear=="finland2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN2015"])
polparties$election_id[polparties$country_electionyear=="finland2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FIN2019"])

#France
polparties$election_id[polparties$country_electionyear=="france1958"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1958"])
polparties$election_id[polparties$country_electionyear=="france1962"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1962"])
polparties$election_id[polparties$country_electionyear=="france1967"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1967"])
polparties$election_id[polparties$country_electionyear=="france1968"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1968"])
polparties$election_id[polparties$country_electionyear=="france1973"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1973"])
polparties$election_id[polparties$country_electionyear=="france1978"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1978"])
polparties$election_id[polparties$country_electionyear=="france1981"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1981"])
polparties$election_id[polparties$country_electionyear=="france1986"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1986"])
polparties$election_id[polparties$country_electionyear=="france1988"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1988"])
polparties$election_id[polparties$country_electionyear=="france1993"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1993"])
polparties$election_id[polparties$country_electionyear=="france1997"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA1997"])
polparties$election_id[polparties$country_electionyear=="france2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA2002"])
polparties$election_id[polparties$country_electionyear=="france2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA2007"])
polparties$election_id[polparties$country_electionyear=="france2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA2012"])
polparties$election_id[polparties$country_electionyear=="france2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="FRA2017"])

#Germany
polparties$election_id[polparties$country_electionyear=="germany1957"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1957"])
polparties$election_id[polparties$country_electionyear=="germany1961"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1961"])
polparties$election_id[polparties$country_electionyear=="germany1965"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1965"])
polparties$election_id[polparties$country_electionyear=="germany1969"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1969"])
polparties$election_id[polparties$country_electionyear=="germany1972"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1972"])
polparties$election_id[polparties$country_electionyear=="germany1976"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1976"])
polparties$election_id[polparties$country_electionyear=="germany1980"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1980"])
polparties$election_id[polparties$country_electionyear=="germany1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1983"])
polparties$election_id[polparties$country_electionyear=="germany1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1987"])
polparties$election_id[polparties$country_electionyear=="germany1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1990"])
polparties$election_id[polparties$country_electionyear=="germany1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1994"])
polparties$election_id[polparties$country_electionyear=="germany1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU1998"])
polparties$election_id[polparties$country_electionyear=="germany2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU2002"])
polparties$election_id[polparties$country_electionyear=="germany2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU2005"])
polparties$election_id[polparties$country_electionyear=="germany2009"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU2009"])
polparties$election_id[polparties$country_electionyear=="germany2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU2013"])
polparties$election_id[polparties$country_electionyear=="germany2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU2017"])
polparties$election_id[polparties$country_electionyear=="germany2021"] <- mean(polpargov$election_id[polpargov$country_electionyear=="DEU2021"])

#Grece
polparties$election_id[polparties$country_electionyear=="greece1974"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1974"])
polparties$election_id[polparties$country_electionyear=="greece1977"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1977"])
polparties$election_id[polparties$country_electionyear=="greece1981"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1981"])
polparties$election_id[polparties$country_electionyear=="greece1985"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1985"])
polparties$election_id[polparties$country_electionyear=="greece1989"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1989"])
polparties$election_id[polparties$country_electionyear=="greece1989.5"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1989.5"])
polparties$election_id[polparties$country_electionyear=="greece1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1990"])
polparties$election_id[polparties$country_electionyear=="greece1993"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1993"])
polparties$election_id[polparties$country_electionyear=="greece1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC1996"])
polparties$election_id[polparties$country_electionyear=="greece2000"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2000"])
polparties$election_id[polparties$country_electionyear=="greece2004"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2004"])
polparties$election_id[polparties$country_electionyear=="greece2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2007"])
polparties$election_id[polparties$country_electionyear=="greece2009"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2009"])
polparties$election_id[polparties$country_electionyear=="greece2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2012"])
polparties$election_id[polparties$country_electionyear=="greece2012.5"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2012.5"])
polparties$election_id[polparties$country_electionyear=="greece2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2015"])
polparties$election_id[polparties$country_electionyear=="greece2015.5"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2015.5"])
polparties$election_id[polparties$country_electionyear=="gr  2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GRC2019"])

#Hungary
polparties$election_id[polparties$country_electionyear=="hungary1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN1990"])
polparties$election_id[polparties$country_electionyear=="hungary1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN1994"])
polparties$election_id[polparties$country_electionyear=="hungary1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN1998"])
polparties$election_id[polparties$country_electionyear=="hungary2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN2002"])
polparties$election_id[polparties$country_electionyear=="hungary2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN2006"])
polparties$election_id[polparties$country_electionyear=="hungary2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN2010"])
polparties$election_id[polparties$country_electionyear=="hungary2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN2014"])
polparties$election_id[polparties$country_electionyear=="hungary2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="HUN2018"])

#Ireland
polparties$election_id[polparties$country_electionyear=="ireland1957"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1957"])
polparties$election_id[polparties$country_electionyear=="ireland1961"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1961"])
polparties$election_id[polparties$country_electionyear=="ireland1965"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1965"])
polparties$election_id[polparties$country_electionyear=="ireland1969"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1969"])
polparties$election_id[polparties$country_electionyear=="ireland1973"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1973"])
polparties$election_id[polparties$country_electionyear=="ireland1977"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1977"])
polparties$election_id[polparties$country_electionyear=="ireland1981"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1981"])
polparties$election_id[polparties$country_electionyear=="ireland1982"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1982"])
polparties$election_id[polparties$country_electionyear=="ireland1982.5"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1982.5"])
polparties$election_id[polparties$country_electionyear=="ireland1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1987"])
polparties$election_id[polparties$country_electionyear=="ireland1989"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1989"])
polparties$election_id[polparties$country_electionyear=="ireland1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1992"])
polparties$election_id[polparties$country_electionyear=="ireland1997"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL1997"])
polparties$election_id[polparties$country_electionyear=="ireland2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL2002"])
polparties$election_id[polparties$country_electionyear=="ireland2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL2007"])
polparties$election_id[polparties$country_electionyear=="ireland2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL2011"])
polparties$election_id[polparties$country_electionyear=="ireland2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL2016"])
polparties$election_id[polparties$country_electionyear=="ireland2020"] <- mean(polpargov$election_id[polpargov$country_electionyear=="IRL2020"])

#Italy
polparties$election_id[polparties$country_electionyear=="italy1958"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1958"])
polparties$election_id[polparties$country_electionyear=="italy1963"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1963"])
polparties$election_id[polparties$country_electionyear=="italy1968"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1968"])
polparties$election_id[polparties$country_electionyear=="italy1972"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1972"])
polparties$election_id[polparties$country_electionyear=="italy1976"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1976"])
polparties$election_id[polparties$country_electionyear=="italy1979"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1979"])
polparties$election_id[polparties$country_electionyear=="italy1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1983"])
polparties$election_id[polparties$country_electionyear=="italy1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1987"])
polparties$election_id[polparties$country_electionyear=="italy1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1992"])
polparties$election_id[polparties$country_electionyear=="italy1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1994"])
polparties$election_id[polparties$country_electionyear=="italy1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA1996"])
polparties$election_id[polparties$country_electionyear=="italy2001"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA2001"])
polparties$election_id[polparties$country_electionyear=="italy2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA2006"])
polparties$election_id[polparties$country_electionyear=="italy2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA2008"])
polparties$election_id[polparties$country_electionyear=="italy2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA2013"])
polparties$election_id[polparties$country_electionyear=="italy2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ITA2018"])

#Latvia
polparties$election_id[polparties$country_electionyear=="latvia1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA1990"])
polparties$election_id[polparties$country_electionyear=="latvia1993"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA1993"])
polparties$election_id[polparties$country_electionyear=="latvia1995"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA1995"])
polparties$election_id[polparties$country_electionyear=="latvia1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA1998"])
polparties$election_id[polparties$country_electionyear=="latvia2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA2002"])
polparties$election_id[polparties$country_electionyear=="latvia2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA2006"])
polparties$election_id[polparties$country_electionyear=="latvia2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA2010"])
polparties$election_id[polparties$country_electionyear=="latvia2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA2011"])
polparties$election_id[polparties$country_electionyear=="latvia2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA2014"])
polparties$election_id[polparties$country_electionyear=="latvia2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LVA2018"])

#Lithuania
polparties$election_id[polparties$country_electionyear=="lithuania1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU1990"])
polparties$election_id[polparties$country_electionyear=="lithuania1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU1992"])
polparties$election_id[polparties$country_electionyear=="lithuania1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU1996"])
polparties$election_id[polparties$country_electionyear=="lithuania2000"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU2000"])
polparties$election_id[polparties$country_electionyear=="lithuania2004"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU2004"])
polparties$election_id[polparties$country_electionyear=="lithuania2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU2008"])
polparties$election_id[polparties$country_electionyear=="lithuania2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU2012"])
polparties$election_id[polparties$country_electionyear=="lithuania2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LTU2016"])

#Luxembourg
polparties$election_id[polparties$country_electionyear=="luxembourg1959"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX1959"])
polparties$election_id[polparties$country_electionyear=="luxembourg1964"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX1964"])
polparties$election_id[polparties$country_electionyear=="luxembourg1968"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX1968"])
polparties$election_id[polparties$country_electionyear=="luxembourg1974"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX1974"])
polparties$election_id[polparties$country_electionyear=="luxembourg1979"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX1979"])
polparties$election_id[polparties$country_electionyear=="luxembourg1984"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX1984"])
polparties$election_id[polparties$country_electionyear=="luxembourg1989"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX1989"])
polparties$election_id[polparties$country_electionyear=="luxembourg1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX1994"])
polparties$election_id[polparties$country_electionyear=="luxembourg1999"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX1999"])
polparties$election_id[polparties$country_electionyear=="luxembourg2004"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX2004"])
polparties$election_id[polparties$country_electionyear=="luxembourg2009"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX2009"])
polparties$election_id[polparties$country_electionyear=="luxembourg2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX2013"])
polparties$election_id[polparties$country_electionyear=="luxembourg2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="LUX2018"])

#Malta
polparties$election_id[polparties$country_electionyear=="malta1962"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT1962"])
polparties$election_id[polparties$country_electionyear=="malta1966"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT1966"])
polparties$election_id[polparties$country_electionyear=="malta1971"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT1971"])
polparties$election_id[polparties$country_electionyear=="malta1976"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT1976"])
polparties$election_id[polparties$country_electionyear=="malta1981"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT1981"])
polparties$election_id[polparties$country_electionyear=="malta1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT1987"])
polparties$election_id[polparties$country_electionyear=="malta1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT1992"])
polparties$election_id[polparties$country_electionyear=="malta1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT1996"])
polparties$election_id[polparties$country_electionyear=="malta1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT1998"])
polparties$election_id[polparties$country_electionyear=="malta2003"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT2003"])
polparties$election_id[polparties$country_electionyear=="malta2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT2008"])
polparties$election_id[polparties$country_electionyear=="malta2013"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT2013"])
polparties$election_id[polparties$country_electionyear=="malta2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="MLT2017"])

#Netherlands
polparties$election_id[polparties$country_electionyear=="netherlands1959"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1959"])
polparties$election_id[polparties$country_electionyear=="netherlands1963"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1963"])
polparties$election_id[polparties$country_electionyear=="netherlands1967"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1967"])
polparties$election_id[polparties$country_electionyear=="netherlands1971"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1971"])
polparties$election_id[polparties$country_electionyear=="netherlands1972"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1972"])
polparties$election_id[polparties$country_electionyear=="netherlands1977"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1977"])
polparties$election_id[polparties$country_electionyear=="netherlands1981"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1981"])
polparties$election_id[polparties$country_electionyear=="netherlands1982"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1982"])
polparties$election_id[polparties$country_electionyear=="netherlands1986"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1986"])
polparties$election_id[polparties$country_electionyear=="netherlands1989"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1989"])
polparties$election_id[polparties$country_electionyear=="netherlands1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1994"])
polparties$election_id[polparties$country_electionyear=="netherlands1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD1998"])
polparties$election_id[polparties$country_electionyear=="netherlands2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD2002"])
polparties$election_id[polparties$country_electionyear=="netherlands2003"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD2003"])
polparties$election_id[polparties$country_electionyear=="netherlands2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD2006"])
polparties$election_id[polparties$country_electionyear=="netherlands2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD2010"])
polparties$election_id[polparties$country_electionyear=="netherlands2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD2012"])
polparties$election_id[polparties$country_electionyear=="netherlands2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="NLD2017"])

#Poland
polparties$election_id[polparties$country_electionyear=="poland1989"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL1989"])
polparties$election_id[polparties$country_electionyear=="poland1991"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL1991"])
polparties$election_id[polparties$country_electionyear=="poland1993"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL1993"])
polparties$election_id[polparties$country_electionyear=="poland1997"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL1997"])
polparties$election_id[polparties$country_electionyear=="poland2001"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2001"])
polparties$election_id[polparties$country_electionyear=="poland2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2005"])
polparties$election_id[polparties$country_electionyear=="poland2007"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2007"])
polparties$election_id[polparties$country_electionyear=="poland2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2011"])
polparties$election_id[polparties$country_electionyear=="poland2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2015"])
polparties$election_id[polparties$country_electionyear=="poland2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="POL2019"])

#Portugal
polparties$election_id[polparties$country_electionyear=="portugal1975"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1975"])
polparties$election_id[polparties$country_electionyear=="portugal1976"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1976"])
polparties$election_id[polparties$country_electionyear=="portugal1979"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1979"])
polparties$election_id[polparties$country_electionyear=="portugal1980"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1980"])
polparties$election_id[polparties$country_electionyear=="portugal1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1983"])
polparties$election_id[polparties$country_electionyear=="portugal1985"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1985"])
polparties$election_id[polparties$country_electionyear=="portugal1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1987"])
polparties$election_id[polparties$country_electionyear=="portugal1991"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1991"])
polparties$election_id[polparties$country_electionyear=="portugal1995"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1995"])
polparties$election_id[polparties$country_electionyear=="portugal1999"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT1999"])
polparties$election_id[polparties$country_electionyear=="portugal2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2002"])
polparties$election_id[polparties$country_electionyear=="portugal2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2005"])
polparties$election_id[polparties$country_electionyear=="portugal2009"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2009"])
polparties$election_id[polparties$country_electionyear=="portugal2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2011"])
polparties$election_id[polparties$country_electionyear=="portugal2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2015"])
polparties$election_id[polparties$country_electionyear=="portugal2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="PRT2019"])

#Romania
polparties$election_id[polparties$country_electionyear=="romania1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU1990"])
polparties$election_id[polparties$country_electionyear=="romania1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU1992"])
polparties$election_id[polparties$country_electionyear=="romania1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU1996"])
polparties$election_id[polparties$country_electionyear=="romania2000"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU2000"])
polparties$election_id[polparties$country_electionyear=="romania2004"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU2004"])
polparties$election_id[polparties$country_electionyear=="romania2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU2008"])
polparties$election_id[polparties$country_electionyear=="romania2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU2012"])
polparties$election_id[polparties$country_electionyear=="romania2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="ROU2016"])

#Slovenia
polparties$election_id[polparties$country_electionyear=="slovenia1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN1990"])
polparties$election_id[polparties$country_electionyear=="slovenia1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN1992"])
polparties$election_id[polparties$country_electionyear=="slovenia1996"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN1996"])
polparties$election_id[polparties$country_electionyear=="slovenia2000"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN2000"])
polparties$election_id[polparties$country_electionyear=="slovenia2004"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN2004"])
polparties$election_id[polparties$country_electionyear=="slovenia2008"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN2008"])
polparties$election_id[polparties$country_electionyear=="slovenia2011"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN2011"])
polparties$election_id[polparties$country_electionyear=="slovenia2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN2014"])
polparties$election_id[polparties$country_electionyear=="slovenia2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVN2018"])

#Slovakia
polparties$election_id[polparties$country_electionyear=="slovakia1990"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK1990"])
polparties$election_id[polparties$country_electionyear=="slovakia1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK1992"])
polparties$election_id[polparties$country_electionyear=="slovakia1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK1994"])
polparties$election_id[polparties$country_electionyear=="slovakia1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK1998"])
polparties$election_id[polparties$country_electionyear=="slovakia2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK2002"])
polparties$election_id[polparties$country_electionyear=="slovakia2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK2006"])
polparties$election_id[polparties$country_electionyear=="slovakia2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK2010"])
polparties$election_id[polparties$country_electionyear=="slovakia2012"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK2012"])
polparties$election_id[polparties$country_electionyear=="slovakia2016"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK2016"])
polparties$election_id[polparties$country_electionyear=="slovakia2020"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SVK2020"])

#Sweden
polparties$election_id[polparties$country_electionyear=="sweden1958"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1958"])
polparties$election_id[polparties$country_electionyear=="sweden1960"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1960"])
polparties$election_id[polparties$country_electionyear=="sweden1964"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1964"])
polparties$election_id[polparties$country_electionyear=="sweden1968"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1968"])
polparties$election_id[polparties$country_electionyear=="sweden1970"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1970"])
polparties$election_id[polparties$country_electionyear=="sweden1973"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1973"])
polparties$election_id[polparties$country_electionyear=="sweden1976"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1976"])
polparties$election_id[polparties$country_electionyear=="sweden1979"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1979"])
polparties$election_id[polparties$country_electionyear=="sweden1982"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1982"])
polparties$election_id[polparties$country_electionyear=="sweden1985"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1985"])
polparties$election_id[polparties$country_electionyear=="sweden1988"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1988"])
polparties$election_id[polparties$country_electionyear=="sweden1991"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1991"])
polparties$election_id[polparties$country_electionyear=="sweden1994"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1994"])
polparties$election_id[polparties$country_electionyear=="sweden1998"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE1998"])
polparties$election_id[polparties$country_electionyear=="sweden2002"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE2002"])
polparties$election_id[polparties$country_electionyear=="sweden2006"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE2006"])
polparties$election_id[polparties$country_electionyear=="sweden2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE2010"])
polparties$election_id[polparties$country_electionyear=="sweden2014"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE2014"])
polparties$election_id[polparties$country_electionyear=="sweden2018"] <- mean(polpargov$election_id[polpargov$country_electionyear=="SWE2018"])

#United Kingdom
polparties$election_id[polparties$country_electionyear=="united kingdom1959"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1959"])
polparties$election_id[polparties$country_electionyear=="united kingdom1964"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1964"])
polparties$election_id[polparties$country_electionyear=="united kingdom1966"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1966"])
polparties$election_id[polparties$country_electionyear=="united kingdom1970"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1970"])
polparties$election_id[polparties$country_electionyear=="united kingdom1974"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1974"])
polparties$election_id[polparties$country_electionyear=="united kingdom1979"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1979"])
polparties$election_id[polparties$country_electionyear=="united kingdom1983"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1983"])
polparties$election_id[polparties$country_electionyear=="united kingdom1987"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1987"])
polparties$election_id[polparties$country_electionyear=="united kingdom1992"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1992"])
polparties$election_id[polparties$country_electionyear=="united kingdom1997"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR1997"])
polparties$election_id[polparties$country_electionyear=="united kingdom2001"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR2001"])
polparties$election_id[polparties$country_electionyear=="united kingdom2005"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR2005"])
polparties$election_id[polparties$country_electionyear=="united kingdom2010"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR2010"])
polparties$election_id[polparties$country_electionyear=="united kingdom2015"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR2015"])
polparties$election_id[polparties$country_electionyear=="united kingdom2017"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR2017"])
polparties$election_id[polparties$country_electionyear=="united kingdom2019"] <- mean(polpargov$election_id[polpargov$country_electionyear=="GBR2019"])

# COUNTRY ID - PARLGOV
polparties$country_id <- 0

polparties$country_id[polparties$country=="austria"] <- mean(polpargov$country_id[polpargov$country_name_short=="AUT"])
polparties$country_id[polparties$country=="belgium"] <- mean(polpargov$country_id[polpargov$country_name_short=="BEL"])
polparties$country_id[polparties$country=="bulgaria"] <- mean(polpargov$country_id[polpargov$country_name_short=="BGR"])
polparties$country_id[polparties$country=="croatia"] <- mean(polpargov$country_id[polpargov$country_name_short=="HRV"])
polparties$country_id[polparties$country=="cyprus"] <- mean(polpargov$country_id[polpargov$country_name_short=="CYP"])
polparties$country_id[polparties$country=="czech republic"] <- mean(polpargov$country_id[polpargov$country_name_short=="CZE"])
polparties$country_id[polparties$country=="denmark"] <- mean(polpargov$country_id[polpargov$country_name_short=="DNK"])
polparties$country_id[polparties$country=="spain"] <- mean(polpargov$country_id[polpargov$country_name_short=="ESP"])
polparties$country_id[polparties$country=="estonia"] <- mean(polpargov$country_id[polpargov$country_name_short=="EST"])
polparties$country_id[polparties$country=="finland"] <- mean(polpargov$country_id[polpargov$country_name_short=="FIN"])
polparties$country_id[polparties$country=="france"] <- mean(polpargov$country_id[polpargov$country_name_short=="FRA"])
polparties$country_id[polparties$country=="germany"] <- mean(polpargov$country_id[polpargov$country_name_short=="DEU"])
polparties$country_id[polparties$country=="greece"] <- mean(polpargov$country_id[polpargov$country_name_short=="GRC"])
polparties$country_id[polparties$country=="hungary"] <- mean(polpargov$country_id[polpargov$country_name_short=="HUN"])
polparties$country_id[polparties$country=="ireland"] <- mean(polpargov$country_id[polpargov$country_name_short=="IRL"])
polparties$country_id[polparties$country=="italy"] <- mean(polpargov$country_id[polpargov$country_name_short=="ITA"])
polparties$country_id[polparties$country=="latvia"] <- mean(polpargov$country_id[polpargov$country_name_short=="LVA"])
polparties$country_id[polparties$country=="lithuania"] <- mean(polpargov$country_id[polpargov$country_name_short=="LTU"])
polparties$country_id[polparties$country=="luxembourg"] <- mean(polpargov$country_id[polpargov$country_name_short=="LUX"])
polparties$country_id[polparties$country=="malta"] <- mean(polpargov$country_id[polpargov$country_name_short=="MLT"])
polparties$country_id[polparties$country=="netherlands"] <- mean(polpargov$country_id[polpargov$country_name_short=="NLD"])
polparties$country_id[polparties$country=="poland"] <- mean(polpargov$country_id[polpargov$country_name_short=="POL"])
polparties$country_id[polparties$country=="portugal"] <- mean(polpargov$country_id[polpargov$country_name_short=="PRT"])
polparties$country_id[polparties$country=="romania"] <- mean(polpargov$country_id[polpargov$country_name_short=="ROU"])
polparties$country_id[polparties$country=="slovenia"] <- mean(polpargov$country_id[polpargov$country_name_short=="SVN"])
polparties$country_id[polparties$country=="slovakia"] <- mean(polpargov$country_id[polpargov$country_name_short=="SVK"])
polparties$country_id[polparties$country=="sweden"] <- mean(polpargov$country_id[polpargov$country_name_short=="SWE"])
polparties$country_id[polparties$country=="united kingdom"] <- mean(polpargov$country_id[polpargov$country_name_short=="GBR"])

setwd("")
partyPGMARPOR <- read.csv2("partyfacts_MARPOR_PARLGOV.csv")

#PARTY ID - PARLGOV
partyPGMARPOR <- subset(partyPGMARPOR, select =c(party_id, dataset_party_id.y, partyfacts_id))
polparties <- merge(polparties, partyPGMARPOR, by = "party_id", all = TRUE)
polparties <- filter(polparties, !is.na(country))

setwd("")
polparties$party <- substr(polparties$party, 1, 120)
write_sav(polparties, "PoliticalParties_MARPOR.sav")
write.csv2(polparties, "PoliticalParties_MARPOR.csv")
